package com.hqyj.onlineExamination.modules.exam.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.onlineExamination.modules.common.entity.ResultEntity;
import com.hqyj.onlineExamination.modules.common.entity.SearchBean;
import com.hqyj.onlineExamination.modules.exam.entity.Question;
import com.hqyj.onlineExamination.modules.exam.service.QuestionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yun
 * @date 2021-5-25 14:03
 * 问题列表
 */
@RestController
@RequestMapping("/api")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 127.0.0.1/api/questions ---- post
     * {"currentPage":"1","pageSize":"5","orderBy":"id","direction":"desc","keyWord":""}
     */
    @PostMapping(value = "/questions", consumes = "application/json")
    public PageInfo<Question> selectAllQuestion(@RequestBody SearchBean searchBean) {
        return questionService.selectAllQuestionInPage(searchBean);
    }

    /**
     * 127.0.0.1/api/question/1 ---- get
     */
    @GetMapping("/question/{id}")
    public Question selectQuestionById(@PathVariable int id){
        return questionService.selectQuestionById(id);
    }

    /**
     * 127.0.0.1:81/api/questions ---- get
     */
    @GetMapping("/questions")
    public List<Question> getTypes() {
        return questionService.getTypes();
    }

    /**
     * 127.0.0.1:81/api/questions/singleChoice ---- get
     */
    @GetMapping("/questions/{type}")
    public List<Question> getQuestionsByType(@PathVariable String type) {
        return questionService.getQuestionsByType(type);
    }

    /**
     * 127.0.0.1/api/question ---- post
     * {"type":"baiyun", "flag":"111111","score":"11","content":"11","optionA":"11","optionB":"111111","optionC":"11","optionD":"11","referenceAnswer":"baiyun", "comment":"111111","optiona":"11"}
     */
    @PostMapping(value = "/question", consumes = "application/json")
    @Transactional
    public ResultEntity<Question> addQuestionByQuestion(@RequestBody Question question){
        return questionService.addQuestionByQuestion(question);
    }

    /**
     * 127.0.0.1/api/question/60 ---- delete
     */
    @DeleteMapping(value = "/question/{id}")
    public ResultEntity<Question> deleteUserById(@PathVariable int id){
        return questionService.deleteQuestionById(id);
    }

    /**
     * 127.0.0.1/api/question ---- put
     * {"id":"60","type":"baiyun", "flag":"1","score":"1","content":"1","optionA":"1","optionB":"1","optionC":"1","optionD":"1","referenceAnswer":"baiyun", "comment":"1","optiona":"1"}
     */
    @PutMapping(value = "/question", consumes = "application/json")
    @Transactional
    public ResultEntity<Question> updateQuestionByQuestion(@RequestBody Question question){
        return questionService.updateQuestionByQuestion(question);
    }

    /**
     * 127.0.0.1/api/questionList ---- get
     */
    @GetMapping("/questionList")
    public List<Question> selectAllQuestion(){
        return questionService.selectAllQuestionTypeNum();
    }
}
